<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="utf-8">
    <title>注册界面</title>
    <script th:src="@{/layui/layui.js}"></script>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <script th:src="@{/layui/layui.all.js}"></script>
</head>
<body>
<div style="background-color: #f2f2f2">
    <form class="layui-form" method="post" th:action="@{/sys/Register}" style="width: 800px;height: 800px;margin-left: 500px">
        <div style="margin-bottom: 30px;margin-left: 100px">
            <h2 style=" font-weight: 300;font-size: 30px;color: #000;">Imwenwen注册界面系统</h2>
            <p style="color:red;" th:text="${regerror}" th:if="${not #strings.isEmpty(regerror)}"></p>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" id="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input" style="width: 190px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" id="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="repass" id="repass"  required lay-verify="required" placeholder="确认密码"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-inline">
                <input type="text" name="nickname" id="nickname" required lay-verify="required" placeholder="请输入昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block" style="width:100px;">
                <select name="age" id="age" lay-verify="required">
                    <option value=""></option>
                        <option th:each="i:${#numbers.sequence(1,150)}" th:text="${i}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="telephone" id="telephone" required lay-verify="phone" placeholder="请输入电话号码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-inline">
                <input type="text" value="" id="pic_1_src" name="head" style="display: none;">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="pic_1" width="100" height="100" >
                        <p id="picText_1"></p>
                    </div>
                    <button type="button" class="layui-btn" id="upload_1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">滑动验证</label>
            <div class="layui-input-block" style="width: 190px">
                <div id="slider" ></div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-left:50px">
            <input type="checkbox" name="agreement" id="agreement" lay-skin="primary" title="同意用户协议" required  >
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn " lay-submit lay-filter="LAY-user-reg-submit" style="width: 100px">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/layui/layui.all.js}"></script>
<script>
    layui.config({
        base: '/dist/sliderVerify/'
    }).extend({
        sliderVerify:'sliderVerify'
    });
    layui.use(['sliderVerify', 'jquery', 'form','upload'], function() {
        const $ = layui.$
            , form = layui.form,
            sliderVerify = layui.sliderVerify
            , upload = layui.upload;
        var slider = sliderVerify.render({
            elem: '#slider',
            onOk: function(){//当验证通过回调
                layer.msg("滑块验证通过");
            }
        })
        $('#reset').on('click',function(){
            slider.reset();
        })
        //提交
        form.on('submit(LAY-user-reg-submit)', function(obj){
            const field = obj.field;
            //确认密码
            if(field.password !== field.repass){
                layer.msg('两次密码输入不一致');
                return false;
            }
            // if (field.password.length>16||field.password.length<6){
            //     layer.msg('密码格式不对，请输入6到16个字符');
            //     return false;
            // }
            //是否同意用户协议
            if(!field.agreement){
                layer.msg('你必须同意用户协议才能注册');
                return false;
            }
        });
        const uploadInst = upload.render({
            elem: '#upload_1'
            , url: '/sys/uploadPictures'
            , size: 2048
            , before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#pic_1').attr('src', result);
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败' + res);
                }
                //上传成功
                var demoText = $('#picText_1');
                demoText.html('<span style="color: #4cae4c;">上传成功</span>');
                //将后台获取的图片地址传到前台存入隐藏控件中，随着表单一起提交
                document.getElementById("pic_1_src").value = res.data.src;
                console.log("图片地址：" + res.data.src);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    });


</script>
</body>
</html>
